// edge边数据处理
const codeToOperatorMapping = {
  GT: '>',
  GE: '>=',
  LT: '<',
  LE: '<=',
  EQ: '==',
  NEQ: '!=',
  IN: 'in',
  NOT_IN: 'not in',
  LIKE: 'like',
  NOT_LIKE: 'not like',
  IS_EMPTY: 'is empty',
  IS_NOT_EMPTY: 'is not empty'
};

export const formatConditions = (conditions, conjs = []) => {
  let conditionStrings = [];

  for (let i = 0; i < conditions.length; i++) {
    const condition = conditions[i];

    if (
      (condition.left !== undefined && condition.operator !== undefined && condition.operator !== 'IS_EMPTY' && condition.operator !== 'IS_NOT_EMPTY') && condition.right !== undefined ||
      (condition.left !== undefined && condition.operator === 'IS_EMPTY' || condition.operator === 'IS_NOT_EMPTY')
    ) {
      if (
        (condition.left !== '' && condition.operator !== '' && condition.operator !== 'IS_EMPTY' && condition.operator !== 'IS_NOT_EMPTY') && condition.right !== '' ||
        (condition.left !== '' && condition.operator === 'IS_EMPTY' || condition.operator === 'IS_NOT_EMPTY')
      ) {
        if ('conditions' in condition) {
          // 递归处理嵌套的条件
          const nestedString = formatConditions(condition.conditions, condition.conjs);
          conditionStrings.push(nestedString);
        } else {
          // 处理单个完整条件或 operator 为 IS_EMPTY || IS_NOT_EMPTY的情况
          let singleConditionString;
          if (condition.operator === 'IS_EMPTY' || condition.operator === 'IS_NOT_EMPTY') {
            singleConditionString = `${condition.left} ${codeToOperatorMapping[condition.operator]}`;
          } else {
            singleConditionString = `${condition.left} ${codeToOperatorMapping[condition.operator]} ${condition.right}`;
          }
          conditionStrings.push(singleConditionString);
        }

        // 如果还有剩余的连词和下一个完整的条件，则添加连词
        if (i < conditions.length - 1 && i < conjs.length && conjs[i] !== '') {
          conditionStrings.push(`\n${conjs[i]}\n`);
        }
      }
    }
  }

  // 合并所有条件，并移除首尾多余的换行符
  const joinedConditions = conditionStrings.join('').trim().replace(/^\n+/, '').replace(/\n+$/, '');

  return joinedConditions;
};